package com.skytech.component.monitor.enterprise_data_monitoring;

import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.skytech.component.monitor.data.MonitorData;
import com.skytech.component.monitor.data.MonitorDataDAO;
import com.skytech.component.monitor.data.MonitorDataTag;
import com.skytech.component.monitor.data.MonitorDataTagDAO;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.UUID;

import static com.skytech.component.monitor.util.WebClientUtil.getWebClient;

@Service
public class NMGMonitorService implements  CrawlerScript{

    private static final String province = "内蒙古自治区";

    private static final Logger log = LoggerFactory.getLogger(NMGMonitorService.class);

    @Autowired
    private MonitorDataTagDAO monitorDataTagDAO;
    @Autowired
    private MonitorDataDAO monitorDataDAO;


    private Connection formateConnection(Connection connection){
        return connection.timeout(10000)
                .header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8")
                .header("Accept-Encoding", "gzip, deflate, sdch")
                .header("Accept-Language", "zh-CN,zh;q=0.8")
                .header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36");
    }


    public void start() {
        String date = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));

        /*WebClient homePageClient = getWebClient();
        homePageClient.addRequestHeader("Referer", "http://nmgepb.gov.cn:8088/enterprisemonitor/webpage!indexPage.action");*/
        String url = "http://nmgepb.gov.cn:8088/enterprisemonitor/gisnavigation!cityCompanyList.action";
        Connection connection = formateConnection(Jsoup.connect(url));
        connection.header("Referer", "http://nmgepb.gov.cn:8088/enterprisemonitor/webpage!indexPage.action");
       /* HtmlPage homePage =  homePageClient.getPage(url);
        homePageClient.closeAllWindows();*/
        Document document = null;
       try{
           document = connection.get();
       }catch (Exception e){
           log.error(e.getMessage());
       }

        List<String> entIds = new ArrayList<>();


        document.select("a").forEach(l ->{
            String id = l.attr("onclick");
            if(!StringUtils.isEmpty(id)){
                entIds.add(id.split("'")[1]);
            }
            /*String entName = l.attr("title");*/
        });
        MonitorData monitorData;
        List<MonitorData> monitorDataList;
        int j = 0;
        WebClient webClient = getWebClient();
        for (String entId : entIds) {
            log.info("{}--{}企业开始",j,entId);
         /*   try{*/
                // 企业基本信息
            String entInfo = "http://nmgepb.gov.cn:8088/enterprisemonitor/enterprise-info!getCompanyInfo.action?companyid="+entId;
            /*WebClient webClient = getWebClient();*/
            webClient.addRequestHeader("Referer", "http://nmgepb.gov.cn:8088/enterprisemonitor/enterprise-info!getCompanyInfo.action?companyid="+entId);
            HtmlPage page;
            try{
                page =  webClient.getPage(entInfo);
                /*webClient.closeAllWindows();*/
            }catch (Exception e){
                log.error("企业{},发生错误{}",entId,e.getMessage());
                continue;
            }

            Document parse = Jsoup.parse(page.getBody().asXml());
            Elements ele = parse.getElementsMatchingOwnText("企业名称");
            String entName = ele.next().text();
            ele = parse.getElementsMatchingOwnText("企业地址");
            String address = ele.next().text();
            //todo 解析企业信息
            //废水
            String water = "http://nmgepb.gov.cn:8088/enterprisemonitor/enterprise-info!getWasteWaterLists.action?companyid="+entId+"&yesterday="+date;
            webClient = getWebClient();
            webClient.addRequestHeader("Referer", "http://nmgepb.gov.cn:8088/enterprisemonitor/gisnavigation!cityCompanyList.action");
            try{
                page =  webClient.getPage(water);
                /*webClient.closeAllWindows();*/
            }catch (Exception e){
                log.error("企业{},发生错误{}",entId,e.getMessage());
                continue;
            }
            parse = Jsoup.parse(page.getBody().asXml());
            ele = parse.select("tr");
            monitorDataList = new ArrayList<>();
            Collections.reverse(ele);
            String waterUniqueKey = entName+"--"+"water";
            MonitorDataTag monitorDataTag = new MonitorDataTag();
            monitorDataTag.setProvince(province);
            monitorDataTag.setUniqueKey(waterUniqueKey);
            List<MonitorDataTag> waterHistory = monitorDataTagDAO.findAll(Example.of(monitorDataTag));
            if(!ele.isEmpty()){
                for (int i = 0; i < ele.size(); i++) {
                    if(i!=(ele.size()-1)){
                        //todo parse
                        Elements tds = ele.get(i).getElementsByTag("td");
                        monitorData = new MonitorData();
                        monitorData.setId(UUID.randomUUID().toString())
                                .setCompanyName(entName)
                                .setAddress(address)
                                .setProvince(province)
                                .setMonitorPoint(tds.get(0).attr("title"))
                                .setMonitorInfo(tds.get(4).attr("title"))
                                .setMonitorValue(tds.get(5).attr("title"))
                                .setMonitorTime(tds.get(2).attr("title"))
                                .setMonitorType(tds.get(3).attr("title"))
                                .setMonitorStandardValue(tds.get(6).attr("title"))
                                .setOverproof(tds.get(7).attr("title"))
                                .setPollutionType("废水");
                        monitorData.setCrawlingTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
                        monitorDataList.add(monitorData);

                    }
                }
                saveData(monitorDataList,waterHistory,waterUniqueKey);

            }


            //todo 解析废水信息
            monitorDataList = new ArrayList<>();
            //废气体
            String air = "http://nmgepb.gov.cn:8088/enterprisemonitor/enterprise-info!getWasteAirLists.action?companyid="+entId+"&yesterday="+date;


           /* webClient = getWebClient();*/
            webClient.addRequestHeader("Referer", "http://nmgepb.gov.cn:8088/enterprisemonitor/gisnavigation!cityCompanyList.action");
            try{
                page =  webClient.getPage(air);
                /*webClient.closeAllWindows();*/
            }catch (Exception e){
                log.error("企业{},发生错误{}",entId,e.getMessage());
                continue;
            }
            parse = Jsoup.parse(page.getBody().asXml());
            ele = parse.select("tr");


            Collections.reverse(ele);
            String gasUniqueKey = entName+"--"+"gas";
            monitorDataTag = new MonitorDataTag();
            monitorDataTag.setProvince(province);
            monitorDataTag.setUniqueKey(gasUniqueKey);
            List<MonitorDataTag> gasHistory = monitorDataTagDAO.findAll(Example.of(monitorDataTag));
            if(!ele.isEmpty()){
                for (int i = 0; i < ele.size(); i++) {
                    if(i!=(ele.size()-1)){
                        //todo parse
                        Elements tds = ele.get(i).getElementsByTag("td");
                        monitorData = new MonitorData();
                        monitorData.setId(UUID.randomUUID().toString())
                                .setCompanyName(entName)
                                .setAddress(address)
                                .setProvince(province)
                                .setMonitorPoint(tds.get(0).attr("title"))
                                .setMonitorInfo(tds.get(4).attr("title"))
                                .setMonitorValue(tds.get(5).attr("title"))
                                .setMonitorTime(tds.get(2).attr("title"))
                                .setMonitorType(tds.get(3).attr("title"))
                                .setMonitorStandardValue(tds.get(6).attr("title"))
                                .setOverproof(tds.get(7).attr("title"))
                                .setPollutionType("废气");
                        monitorData.setCrawlingTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
                        monitorDataList.add(monitorData);

                    }
                }
                saveData(monitorDataList,gasHistory,gasUniqueKey);
            }



            //todo 解析废气信息
            monitorDataList = new ArrayList<>();
            //
            //噪声
            String noise = "http://nmgepb.gov.cn:8088/enterprisemonitor/enterprise-info!getNoiseLists.action?companyid="+entId+"1&yesterday="+date;
            /*webClient = getWebClient();*/
            webClient.addRequestHeader("Referer", "http://nmgepb.gov.cn:8088/enterprisemonitor/gisnavigation!cityCompanyList.action");
            try{
                page =  webClient.getPage(noise);
                /*webClient.closeAllWindows();*/
            }catch (Exception e){
                log.error("企业{},发生错误{}",entId,e.getMessage());
                continue;
            }
            parse = Jsoup.parse(page.getBody().asXml());
            ele = parse.select("tr");


            Collections.reverse(ele);
            String noiseUniqueKey = entName+"--"+"noise";
            monitorDataTag = new MonitorDataTag();
            monitorDataTag.setProvince(province);
            monitorDataTag.setUniqueKey(noiseUniqueKey);
            List<MonitorDataTag> noiseHistory = monitorDataTagDAO.findAll(Example.of(monitorDataTag));
            if(!ele.isEmpty()){
                for (int i = 0; i < ele.size(); i++) {
                    if(i!=(ele.size()-1)){
                        //todo parse
                        Elements tds = ele.get(i).getElementsByTag("td");
                        monitorData = new MonitorData();
                        monitorData.setId(UUID.randomUUID().toString())
                                .setCompanyName(entName)
                                .setAddress(address)
                                .setProvince(province)
                                .setMonitorPoint(tds.get(0).attr("title"))
                                .setMonitorInfo(tds.get(2).attr("title"))
                                .setMonitorValue(tds.get(3).attr("title"))
                                .setMonitorTime(tds.get(1).attr("title"))
                                .setMonitorType("自动监测")
                                .setMonitorStandardValue(tds.get(4).attr("title"))
                                .setOverproof(tds.get(5).attr("title"))
                                .setPollutionType("噪声");
                        monitorData.setCrawlingTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
                        monitorDataList.add(monitorData);

                    }
                }
                saveData(monitorDataList,noiseHistory,noiseUniqueKey);
            }

            j++;
        }
        webClient.closeAllWindows();
        log.info("总共完成{}条企业数据抓取",j);
    }


    private void saveData(List<MonitorData> monitorDataList,List<MonitorDataTag> monitorDataTagList,String uniqueKey){

        if(!monitorDataList.isEmpty()){
            if(monitorDataTagList.isEmpty()){
                MonitorDataTag monitorDataTag = new MonitorDataTag();
                monitorDataTag.setId(UUID.randomUUID().toString());
                monitorDataTag.setProvince(province);
                monitorDataTag.setUniqueKey(uniqueKey);
                monitorDataTag.setLabel(monitorDataList.get(0).getMonitorPoint()+monitorDataList.get(0).getMonitorTime());
                monitorDataTagDAO.save(monitorDataTag);
                monitorDataDAO.saveAll(monitorDataList);
            }else{
                int k = 0;
                for (MonitorData data : monitorDataList) {
                    if(monitorDataTagList.get(0).getLabel().equals(data.getMonitorPoint()+data.getMonitorTime())){
                        break;
                    }else{
                        monitorDataDAO.save(data);
                        if(k== 0){
                            MonitorDataTag monitorDataTagUpdate = monitorDataTagList.get(0);
                            monitorDataTagUpdate.setLabel(data.getMonitorPoint()+data.getMonitorTime());
                            monitorDataTagDAO.save(monitorDataTagUpdate);
                        }
                        k++;
                    }
                }
            }
        }
    }
}
